package com.zhoug.zmui3.dialog.date;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.StringUtils;
import com.zhoug.common3.utils.TimeUtils;
import com.zhoug.zmui3.R;
import com.zhoug.zmui3.ZMUIAnimations;
import com.zhoug.zmui3.date.DateSelectInterface;
import com.zhoug.zmui3.date.ZMUIDatePicker;
import com.zhoug.zmui3.dialog.ZMUIBaseDialog;

import androidx.annotation.NonNull;

/**
 * 开始日期和结束日期选择窗口
 *
 * @Author: zhoug
 * @Date: 2024-07-18
 * @Description:
 */
public class ZMUIMultiDateChooser extends ZMUIBaseDialog implements View.OnClickListener {
    private static final String TAG = ">>>ZMUIMultiDateChooser";
    private static boolean DEBUG = false;
    public static final int MODE_DATE = DateSelectInterface.MODE_DATE;// 年月日
    public static final int MODE_TIME = DateSelectInterface.MODE_TIME;// 时分
    public static final int MODE_DATE_TIME = DateSelectInterface.MODE_DATE_TIME;//年月日 时分
    private Callback callback;//回掉
    private String startTime;//开始时间
    private String endTime;//结束时间
    private boolean mShowYear = true;
    private boolean mShowMonth = true;
    private boolean mShowDay = true;
    private boolean mShowHour = true;
    private boolean mShowMinute = true;

    private ZMUIDatePicker mDatePicker;
    private TextView mBtnConfirm;
    private TextView mTvStartTime;
    private LinearLayout mBtnStartTime;
    private TextView mTvEndTime;
    private LinearLayout mBtnEndTime;
    private RelativeLayout mContainer;

    public static int TYPE_START = 1;//开始时间
    public static int TYPE_END = 2;//结束时间
    private int type = TYPE_START;//当前选择的是开始时间还是结束时间
    private static final String PATTERN_DATE = TimeUtils.PATTERN_DATE;
    private static final String PATTERN_TIME = TimeUtils.PATTERN_TIME;
    private static final String PATTERN_DATE_TIME = TimeUtils.PATTERN_DATE_TIME;
    private String pattern = PATTERN_DATE;

    public static final int HORIZONTAL = DateSelectInterface.HORIZONTAL;//水平对齐
    public static final int VERTICAL = DateSelectInterface.VERTICAL;//垂直对齐
    /**
     * 对齐方式
     */
    private int mOrientation = HORIZONTAL;

    /**
     * {@link #MODE_DATE,#MODE_TIME}
     */
    private int dateMode = MODE_DATE;
    private TextView mTvStartName;
    private TextView mTvEndName;
    private String startTimeName;//开始日期
    private String endTimeName;


    public ZMUIMultiDateChooser(@NonNull Context context) {
        super(context);
    }


    public void setOrientation(int orientation) {
        this.mOrientation = orientation;
    }

    @Override
    protected int getLayoutResId() {
        if (mOrientation == VERTICAL) {
            return R.layout.zmui_multi_date_chooser_vertical;

        } else {
            return R.layout.zmui_multi_date_chooser_horizontal;
        }
    }

    private void findViews() {
        mDatePicker = findViewById(R.id.datePicker);
        mBtnConfirm = findViewById(R.id.btn_confirm);
        mTvStartTime = findViewById(R.id.tv_startTime);
        mBtnStartTime = findViewById(R.id.btn_startTime);
        mTvEndTime = findViewById(R.id.tv_endTime);
        mBtnEndTime = findViewById(R.id.btn_endTime);
        mContainer = findViewById(R.id.container_23);
        mTvStartName = findViewById(R.id.tv_startName);
        mTvEndName = findViewById(R.id.tv_endName);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setFullWidth(true);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        setGravity(Gravity.BOTTOM);
        setWindowAnimations(ZMUIAnimations.ANIM_DOWN);
        findViews();

        mTvStartName.setText(getStartTimeName());
        mTvEndName.setText(getEndTimeName());

        mDatePicker.setMode(dateMode);
        mDatePicker.setDateFormat(pattern);
        mDatePicker.setOnSelectChangeListener(new ZMUIDatePicker.OnSelectChangeListener() {
            @Override
            public void onDateChange(int year, int month, int day, int hour, int minute) {

            }

            @Override
            public void onDateChange(String date) {
                if (type == TYPE_START) {
                    startTime = date;
                    mTvStartTime.setText(date);
                } else if (type == TYPE_END) {
                    endTime = date;
                    mTvEndTime.setText(date);
                }
            }
        });


        mBtnStartTime.setOnClickListener(this);
        mBtnEndTime.setOnClickListener(this);
        mBtnConfirm.setOnClickListener(this);
    }


    public String getStartTime() {
        //2024-02-12 12:02
        if (startTime != null && !startTime.isEmpty()) {
            if (startTime.contains("-")) {
                String ymd = startTime.substring(0, startTime.lastIndexOf("-"));
                String[] split = ymd.split("-");


            }
        }
        return startTime;
    }

    private void init() {
        exchangeLocation();

        //现在时间
        long currentTimeMillis = System.currentTimeMillis();
        String currentDate = TimeUtils.format(currentTimeMillis, pattern);
        if (type == TYPE_START) {
            //选择开始时间
            if (StringUtils.isNullOrEmpty(startTime)) {
                startTime = currentDate;
            }
            mDatePicker.setSelect(startTime);
        } else if (type == TYPE_END) {
            //选择结束时间
            if (StringUtils.isNullOrEmpty(endTime)) {
                if (StringUtils.isNullOrEmpty(startTime)) {
                    endTime = currentDate;
                } else {
                    endTime = startTime;
                }
            }
            mDatePicker.setSelect(endTime);
        }

        mDatePicker.notifyDataChange();
        mTvStartTime.setText(startTime);
        mTvEndTime.setText(endTime);

        showYear(mShowYear);
        showMonth(mShowMonth);
        showDay(mShowDay);
        showHour(mShowHour);
        showMinute(mShowMinute);
    }


    private String getStartTimeName() {
        if (startTimeName == null) {
            return "开始" + getDateName();
        }
        return startTimeName;
    }

    private String getEndTimeName() {
        if (endTimeName == null) {
            return "结束" + getDateName();
        }
        return endTimeName;
    }

    private String getDateName() {
        if (dateMode == MODE_TIME) {
            return "时间";
        }
        return "日期";
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_startTime) {
            type = TYPE_START;
            init();
        } else if (id == R.id.btn_endTime) {
            type = TYPE_END;
            init();
        } else if (id == R.id.btn_confirm) {
            String startTimeName = getStartTimeName();
            String endTimeName = getEndTimeName();
            if (StringUtils.isNullOrEmpty(startTime)) {
                Toast.makeText(getContext(), StringUtils.format("请选择%s", startTimeName), Toast.LENGTH_SHORT).show();
            } else if (StringUtils.isNullOrEmpty(endTime)) {
                Toast.makeText(getContext(), StringUtils.format("请选择%s", endTimeName), Toast.LENGTH_SHORT).show();
            } else {
                if (TimeUtils.compareTime(startTime, endTime, pattern) > 0) {
                    Toast.makeText(getContext(), StringUtils.format("%s不能大于%s", startTimeName, endTimeName), Toast.LENGTH_SHORT).show();
                    return;
                }


                dismiss();
                if (callback != null) {
                    callback.onSelectDate(startTime, endTime);
                }
            }

        }
    }

    private void exchangeLocation() {
        String tag = mBtnEndTime.getTag().toString();

        if (type == TYPE_START) {
            if (!"1".equals(tag)) {
                if (DEBUG) {
                    LogUtils.d(TAG, "exchangeLocation:交换位置");
                }
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mDatePicker.getLayoutParams();
                params.removeRule(RelativeLayout.BELOW);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                mDatePicker.setLayoutParams(params);


                RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) mBtnEndTime.getLayoutParams();
                params1.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
                params1.addRule(RelativeLayout.BELOW, R.id.datePicker);
                mBtnEndTime.setLayoutParams(params1);
                mBtnEndTime.setTag("1");

//                ObjectAnimator.ofFloat(mDatePicker, "TranslationY", -screenWidth + 50).setDuration(1000).start();


            }
        } else {
            if ("1".equals(tag)) {
                if (DEBUG) {
                    LogUtils.d(TAG, "exchangeLocation:交换位置");
                }
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mBtnEndTime.getLayoutParams();
                params.removeRule(RelativeLayout.BELOW);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                mBtnEndTime.setLayoutParams(params);

                RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) mDatePicker.getLayoutParams();
                params1.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
                params1.addRule(RelativeLayout.BELOW, R.id.btn_endTime);
                mDatePicker.setLayoutParams(params1);
                mBtnEndTime.setTag("0");
            }

        }

    }

    @Override
    public void show() {
        super.show();
        init();
    }

    public void setDateMode(int dateMode) {
        if (this.dateMode != dateMode) {
            this.dateMode = dateMode;
            if (dateMode == MODE_DATE) {
                pattern = PATTERN_DATE;
            } else if (dateMode == MODE_TIME) {
                pattern = PATTERN_TIME;
            } else if (dateMode == MODE_DATE_TIME) {
                pattern = PATTERN_DATE_TIME;
            }
        }

    }

    public int getDateMode() {
        return dateMode;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setStartTimeName(String startTimeName) {
        this.startTimeName = startTimeName;
    }

    public void setEndTimeName(String endTimeName) {
        this.endTimeName = endTimeName;
    }

    public void showYear(boolean show) {
        this.mShowYear = show;
        if (mDatePicker != null) {
            mDatePicker.showYear(show);
        }
    }

    public void showMonth(boolean show) {
        this.mShowMonth = show;
        if (mDatePicker != null) {
            mDatePicker.showMonth(show);
        }
    }

    public void showDay(boolean show) {
        this.mShowDay = show;
        if (mDatePicker != null) {
            mDatePicker.showDay(show);
        }
    }

    public void showHour(boolean show) {
        this.mShowHour = show;
        if (mDatePicker != null) {
            mDatePicker.showHour(show);
        }
    }

    public void showMinute(boolean show) {
        this.mShowMinute = show;
        if (mDatePicker != null) {
            mDatePicker.showMinute(show);
        }
    }


    public interface Callback {
        void onSelectDate(String startTime, String endTime);
    }

}
